package com.bms.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bms.dao.AuthorDao;
import com.bms.entity.Author;

public class AuthorAction extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("进入author的post");
		// 1、先获取暗号
		String method = request.getParameter("method");
		AuthorDao dao = new AuthorDao();
		// 2、对暗号
		if ("add".equals(method)) {// 添加
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");

			// 先封装数据
			Author author = new Author(firstName, lastName);
			dao.insert(author);

			response.sendRedirect("authors/index.jsp");
		} else if ("delete".equals(method)) {// 删除
			String param = request.getParameter("id");
			int id = Integer.valueOf(param);
			dao.deleteAuthorById(id);
			response.sendRedirect("authors/index.jsp");
		} else if ("doEdit".equals(method)) {// 完成修改
			System.out.println("开始修改作者");
			String param = request.getParameter("id");
			int id = Integer.valueOf(param);
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");

			Author author = new Author(id, firstName, lastName);
			dao.update(author);
			response.sendRedirect("authors/index.jsp");
		}
	}

}
